package im.starchat.chat.activity.profile;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.activity.BaseActivity;
import im.starchat.chat.adapter.LiveAdapter;
import im.starchat.chat.control.QavsdkControl;
import im.starchat.chat.dialog.ShareRoomDialogFragment;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.RoomBean;
import im.starchat.chat.utils.Constants;
import im.starchat.chat.utils.PreferencesStorageUtil;
import im.starchat.chat.utils.Util;
import im.starchat.chat.view.swipeRefreshLayout.SwipeRefreshLayout;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;


/**
 * 贡献榜
 */
public class ZhiboListActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    @InjectView(R.id.zhibo_back)
    ImageView mBackBtn;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_refresh_layout_list)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private QavsdkControl mQavsdkControl;

    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    private LiveAdapter mLiveAdapter;

    private LinearLayoutManager mLayoutManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhibo);
        ButterKnife.inject(this);


        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        mBackBtn.setOnClickListener(this);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
//        mAttentionAdapter = new AttentionAdapter(this, StaticUser.mUserList);
        mLiveAdapter = new LiveAdapter(ZhiboListActivity.this, new LiveAdapter.LiveAdapterCallback() {
            @Override
            public void onItemClick(RoomBean feedBean) {
//                if (feedBean != null && feedBean.getUser().getId() == (UserUtils.getUserInstance().getId())) {
//                    Toast.makeText(getActivity(), "你不能加入自己的房间", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                Constants.setChooseRoomInfo(feedBean);//设置房间信息
                createRoom(feedBean.getId());
            }

            @Override
            public void onShareBtnClick(RoomBean feedBean) {
                final ShareRoomDialogFragment shareRoomDialogFragment = ShareRoomDialogFragment.newInstance(feedBean);
                shareRoomDialogFragment.show(getSupportFragmentManager().beginTransaction(), "share_app");
            }
        });
        mRecyclerView.setAdapter(mLiveAdapter);
        mQavsdkControl = XLApplication.getQavsdkControl();
        getFeedList();
        mSwipeRefreshLayout.setRefreshing(true);


    }

    private void createRoom(int num) {
        if (Util.isNetworkAvailable(ZhiboListActivity.this.getApplicationContext())) {
            if (num != 0) {
                int nums = num;
                mQavsdkControl.enterRoom(nums);
                Toast.makeText(ZhiboListActivity.this, "正在进入直播中...", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(ZhiboListActivity.this, getString(R.string.notify_no_network), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getFeedList();
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            getFeedList();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            Toast.makeText(ZhiboListActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
        }
    }

    private void getFeedList(){
        XLApplication.getInstance().getRestClient().getUserRooms(PreferencesStorageUtil.getUserId(ZhiboListActivity.this), mCurrentPage, RestClient.DEFAULT_PREPAGE, new Callback<RoomBean[]>() {
            @Override
            public void success(RoomBean[] feedBeans, Response response) {
                if (mCurrentPage == RestClient.DEFAULT_PAGE) {
                    mLiveAdapter.refreshData(new ArrayList<RoomBean>(Arrays.asList(feedBeans)));
                } else {
                    mLiveAdapter.addAll(new ArrayList<RoomBean>(Arrays.asList(feedBeans)));
                }
                if (feedBeans.length < 20) {
                    mHasMoreData = false;
                }else{
                    mHasMoreData = true;
                }
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }

            @Override
            public void failure(RetrofitError error) {
                mSwipeRefreshLayout.setRefreshing(false);
                mSwipeRefreshLayout.setLoading(false);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.zhibo_back:
                finish();
                break;
        }
    }
}
